home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 036a / pmfinder.zip / SCAN.C < prev    next >
Text File  |  1991-12-05  |  8KB  |  277 lines

  1. /*
  2.  * OS/2 PM File Finder Utility - Thread SOURCE CODE
  3.  *
  4.  * LANGUAGE      : Microsoft C6.0
  5.  * MODEL         : large - MT
  6.  * ENVIRONMENT   : IBM OS/2 PM Toolkit v1.3
  7.  * STATUS        : operational
  8.  *
  9.  * This module contains the code for the worker threads
  10.  * that do the searching
  11.  */
  12.  
  13. #define INCL_DOSPROCESS
  14. #define INCL_WINWINDOWMGR
  15. #define INCL_WINLISTBOXES
  16. #define INCL_WINMESSAGEMGR
  17. #define NUL '\0'
  18.  
  19. #include <os2.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <process.h>
  24.  
  25. #include "scan.h"
  26.  
  27.  
  28. /* global variables */
  29. extern BOOL        fContinue;
  30. extern HSEM  hsemWait;
  31.  
  32.  
  33. /* */
  34. void cdecl FAR ScanThread( PSCANTHREADPARM    pstp)
  35. {
  36. CHAR      temp_str[2];             /* temporary spec  string   */
  37. CHAR      szFileSpec[64];          /* file specification string  */
  38. CHAR      szArchivePattern[12];    /* pattern to search archives */
  39. CHAR      szArchive[4];            /* archive indicator          */
  40. int         i;
  41. static HMQ         hmqScan;        /* handle to message queue  */
  42. static HAB         habScan;        /* handle to anchor block  */
  43.  
  44.  
  45.   fContinue= TRUE;
  46.  
  47.   /* This is a real cheap way to get multithreading.  You should
  48.      not use a message queue if you don't post messages to it.
  49.      You should write the data to a global circular buffer and then
  50.      post a message that data is ready to be written to the listbox.
  51.      I was just too lazy to do so.
  52.      See Stephen Best's OS/2 Threads Cookbook for an implementation
  53.      of such.  Available on BIX and OS/2 Shareware BBS as COOKBO.ZIP */
  54.  
  55.   /* initialize thread & create message queue */
  56.   habScan = WinInitialize( 0 );
  57.   hmqScan = WinCreateMsgQueue( habScan, 0 );
  58.  
  59.         szArchivePattern[0] = NUL;
  60.         strcpy(szArchive,pstp->szArchive);
  61.         /* clear the semaphore, the next thread can use the structure*/
  62.         DosSemClear(hsemWait);
  63.  
  64.        /* for all the drive do the search */
  65.             for (i=0;pstp->drive_spec[i] && i < 27 && fContinue;i++)
  66.              {
  67.                /* create directory listing using pattern */
  68.                temp_str[0] = pstp->drive_spec[i];
  69.                temp_str[1] = NUL;
  70.                strcpy(szFileSpec,temp_str);
  71.                strcat(szFileSpec,":\\");
  72.                /* if this is an archive thread then put the pattern
  73.                   in szArchivePattern and search for *.szArchive */
  74.                if (strlen(szArchive))
  75.                {
  76.                   strcpy(szArchivePattern,strupr(pstp->szPattern));
  77.                   strcat(szFileSpec,"*.");
  78.                   strcat(szFileSpec,strupr(szArchive));
  79.  
  80.                }
  81.                else
  82.                   strcat(szFileSpec,pstp->szPattern);
  83.                fContinue = Directory(
  84.                  szFileSpec,
  85.                  FILE_NORMAL,
  86.                  pstp->hwndListBox,
  87.                  szArchivePattern
  88.                );
  89.             }
  90.        /* Issue thread done message       */
  91.           WinSendMsg(
  92.             pstp->hwndDlg,
  93.             WM_THREAD_DONE,
  94.             NUL,
  95.             NUL
  96.             );
  97.   WinDestroyMsgQueue( hmqScan );
  98.   WinTerminate( habScan );
  99.      _endthread();
  100.  }
  101.  
  102. /*
  103.  * Directory( szFileSpec, fsAttributes, hwndListBox, szArchivePattern ) : BOOL;
  104.  *
  105.  *    szFileSpec       file search specification
  106.  *    fsAttributes     file attributes to search for
  107.  *    hwndListBox      handle to listbox for directory
  108.  *    szArchivePattern the archive  type to search
  109.  *
  110.  * This function searches the disk for files of the specified type
  111.  * and appends the results found to the list box whose handle is
  112.  * provided.  An error is generated and the process aborted if
  113.  * insufficient memory is available for the search.
  114.  * It also will search the specified archive for files matching the
  115.  * pattern
  116.  *
  117.  * A value of TRUE is returned if the directory operation was
  118.  * successful.
  119.  *
  120.  */
  121.  
  122. static BOOL Directory(
  123.   PSZ        szFileSpec,
  124.   USHORT      fsAttributes,
  125.   HWND        hwndListBox,
  126.   PSZ         szArchivePattern )
  127. {
  128.   /* RM - Some of the array sizes may be to small to work with
  129.      the HPFS long file names - */
  130.  
  131.   BOOL        fDirOutput;             /* boolean directory flag       */
  132.   USHORT      iChar;                  /* index into char string       */
  133.   CHAR        szPath[260];             /* path specification string    */
  134.   CHAR        szSpec[64];             /* search specification string  */
  135.   HDIR        hdirSearch;             /* handle to search directory   */
  136.   USHORT      usSearchCount;          /* # of files to search for     */
  137.   FILEFINDBUF findbuf;                /* search result buffer         */
  138.   CHAR        szEntry[260];           /* current file name            */
  139.   CHAR        szFullname[260];        /* full file name and path      */
  140.   SHORT       sErr;                   /* error code                   */
  141.  
  142.   /* initialization */
  143.   fContinue = TRUE;
  144.   fDirOutput = FALSE;
  145.  
  146.   /* Give up timeslice for the other threads */
  147.   DosSleep(1);
  148.  
  149.   /* separate file spec into path and wildcards */
  150.   for ( iChar=strlen(szFileSpec)-1; szFileSpec[iChar]!='\\'; iChar-- );
  151.   strcpy( szPath, szFileSpec );
  152.   szPath[iChar] = '\0';
  153.   strcpy( szSpec, &szFileSpec[iChar+1] );
  154.  
  155.   /* perform search for normal files */
  156.   DosFindClose(hdirSearch);
  157.   hdirSearch = HDIR_CREATE;
  158.   usSearchCount = 1;
  159.   sErr = DosFindFirst(
  160.     szFileSpec,
  161.     &hdirSearch,
  162.     fsAttributes,
  163.     &findbuf,
  164.     sizeof(findbuf),
  165.     &usSearchCount,
  166.     0L
  167.   );
  168.   if ( !sErr ) {
  169.  
  170.     /* repeat until all entries exhausted */
  171.     do {
  172.  
  173.       if ( fContinue ) {
  174.  
  175.          /* If this thread is to search archives, call procedure */
  176.          if (strlen(szArchivePattern))
  177.          {
  178.             strcpy(szFullname,szPath);
  179.             strcat(szFullname,"\\");
  180.             strcat(szFullname,findbuf.achName);
  181.             /* A better way to do this, is to use function
  182.                pointers and the next three ifs would be just
  183.                one call. */
  184.             if( strstr(szFileSpec,".ZIP") )
  185.                DoZip(szArchivePattern,szFullname,hwndListBox);
  186.             if( strstr(szFileSpec,".LZH") )
  187.                DoLzh(szArchivePattern,szFullname,hwndListBox);
  188.             if( strstr(szFileSpec,".ARC") )
  189.                DoArc(szArchivePattern,szFullname,hwndListBox);
  190.             if( strstr(szFileSpec,".PAK") )
  191.                DoArc(szArchivePattern,szFullname,hwndListBox);
  192.          }
  193.          else
  194.          {
  195.            /* output current path and file name */
  196.            sprintf(
  197.              szEntry,
  198.              "%s%s%s",
  199.              szPath,
  200.              "\\",
  201.              findbuf.achName
  202.            );
  203.  
  204.            /* add entry to file list box */
  205.            if ( fContinue )
  206.               AddToListBox(hwndListBox,szEntry);
  207.          }
  208.       }
  209.  
  210.       /* get next entry */
  211.       if ( fContinue )
  212.         sErr = DosFindNext(
  213.           hdirSearch,
  214.           &findbuf,
  215.           sizeof(findbuf),
  216.           &usSearchCount
  217.         );
  218.  
  219.     } while ( fContinue && !sErr );
  220.  
  221.   }
  222.  
  223.   if ( fContinue ) {
  224.  
  225.     /* perform search for sub-directories */
  226.     sprintf( szEntry, "%s\\*.*", szPath );
  227.     DosFindClose(hdirSearch);
  228.     hdirSearch = HDIR_CREATE;
  229.     usSearchCount = 1;
  230.     sErr = DosFindFirst(
  231.       szEntry,
  232.       &hdirSearch,
  233.       FILE_DIRECTORY,
  234.       &findbuf,
  235.       sizeof(findbuf),
  236.       &usSearchCount,
  237.       0L
  238.     );
  239.     if ( !sErr ) {
  240.  
  241.       /* repeat until all entries exhausted */
  242.       do {
  243.  
  244.         /* eliminate special directory entries */
  245.         if ( findbuf.attrFile == FILE_DIRECTORY
  246.         && findbuf.achName[0] != '.' ) {
  247.           sprintf(
  248.             szEntry,
  249.             "%s\\%s\\%s",
  250.             szPath,
  251.             findbuf.achName,
  252.             szSpec
  253.           );
  254.           fContinue = Directory( szEntry, fsAttributes, hwndListBox,
  255.                                                        szArchivePattern);
  256.         }
  257.  
  258.         /* get next entry */
  259.         if ( fContinue )
  260.           sErr = DosFindNext(
  261.             hdirSearch,
  262.             &findbuf,
  263.             sizeof(findbuf),
  264.             &usSearchCount
  265.           );
  266.  
  267.       } while ( fContinue && !sErr );
  268.  
  269.     }
  270.  
  271.   }
  272.  
  273.   /* return final result */
  274.   return fContinue;
  275. }
  276.  
  277.